package com.wsoft.cms.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsoft.cms.entity.CmsAnnouncementReadEntity;
import com.wsoft.cms.manager.CmsAnnouncementReadManager;
import com.wsoft.cms.mapper.CmsAnnouncementReadMapper;
import com.wsoft.cms.query.read.ReadListQuery;
import com.wsoft.cms.service.ICmsAnnouncementReadService;
import com.wsoft.cms.vo.read.AnnoReadVO;
import com.wsoft.core.service.CommonMemberService;
import com.wsoft.core.utils.SqlAuthDisableHolder;
import com.wsoft.core.vo.MemberVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户通告阅读标记表 服务实现类
 * </p>
 *
 * @author juiet
 * @since 2024年11月27日 10:38:44
 */
@Service
public class CmsAnnouncementReadServiceImpl extends ServiceImpl<CmsAnnouncementReadMapper, CmsAnnouncementReadEntity> implements ICmsAnnouncementReadService {

    @Resource
    private CmsAnnouncementReadManager readManager;
    @Resource
    private CommonMemberService memberService;

    @Override
    public PageInfo<CmsAnnouncementReadEntity> listRead(ReadListQuery query) {
        SqlAuthDisableHolder.open();
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        PageInfo pageInfo = PageInfo.of(readManager.listRead(query));
        List<CmsAnnouncementReadEntity> list = pageInfo.getList();

        List<AnnoReadVO> voList = new ArrayList<>();

        if(CollUtil.isNotEmpty(list)){
            List<MemberVO> memberList = memberService.getMemberList();
            Map<Long, MemberVO> collect = memberList.stream().collect(Collectors.toMap(MemberVO::getId, memberVO -> memberVO));
            for (CmsAnnouncementReadEntity readEntity : list) {
                AnnoReadVO convert = Convert.convert(AnnoReadVO.class, readEntity);
                MemberVO memberVO = collect.get(convert.getMemberId());
                convert.setMemberName( memberVO == null? "":memberVO.getName());
                convert.setMemberPhoto( memberVO == null? "":memberVO.getPhoto());
                voList.add(convert);
            }
        }
        SqlAuthDisableHolder.close();
        pageInfo.setList(voList);
        return pageInfo;
    }
}
